<?php

class Photo_model extends CI_Model
{
	private $table = 'photo';
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function insert($data)
	{
		$this->db->insert($this->table, $data);
		
		return TRUE;
	}
	
	public function update($data, $photo_id)
	{
		$this->db->where('id', $photo_id);
		$this->db->update($this->table, $data);
		
		return TRUE;
	}
	
	public function getPhoto($photo_id)
	{
		$this->db->where('id', $photo_id);
		$query = $this->db->get($this->table);
		$result = $query->row_array();
		
		return $result;
		
	}
	
	public function insertAlbum($data)
	{
		$this->db->insert('photo_album', $data);
		
		return TRUE;
	}
	
	public function updateAlbum($data, $album_id)
	{
		$this->db->where('id', $album_id);
		$this->db->update('photo_album', $data);
		
		return TRUE;
	}
	
	public function getAlbums()
	{
		$this->db->order_by('name', 'ASC');
		$query = $this->db->get('photo_album');
		$results = $query->result_array();
		
		return $results;
		
	}
	
	public function getAlbum($album_id)
	{
		$this->db->where('id', $album_id);
		$query = $this->db->get('photo_album');
		$results = $query->row_array();
		
		return $results;
		
	}
	
	public function countPhoto($album_id)
	{
		$this->db->where('album_id', $album_id);
		$query = $this->db->count_all_results($this->table);
		
		return $query;
		
	}
	
	public function getAll($limit, $offset, $album_id)
	{
		$query = $this->db->select(array('id', 'name', 'source', 'description'))->from($this->table)->where('album_id', $album_id)->limit($limit, $offset)->order_by('id', 'DESC');
		
		//return $this->db->get()->result_array();
		return $query->get()->result_array();
	}
	
	public function getTotalRows($album_id)
	{
		$this->db->where('album_id', $album_id);
		$query = $this->db->count_all_results($this->table);
		
		return $query;
	}
	
	public function getImage($id)
	{
		$query = $this->db->query("SELECT `photo`, `photo_type` FROM " . $this->table . " WHERE `id` = {$id}");
		
		return $query->row_array();
	}
	
	function deleteSelectedPhoto($pid)
	{
		$this->db->delete($this->table, array('id' => $pid));
		
		return TRUE;
	}
}